package com.tjl.foodapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    public static SharedPreferences sp;

    public static int user_id = 1;
    public static Map<Integer,Map<Integer,Integer>> cart = new HashMap<>();
//初始化成员变量
    RadioButton rbt_shouye_bottom,rbt_collect_bottom,
        rbt_search_bottom,rbt_me_bottom;
    //声明子页面部分
    Fragment shopListFragment,collectFragment,searchFragment,meFragment;
    //声明子页面管理器
    FragmentManager manager;
//图片和按钮的ID数组
    int[] img1 = {R.drawable.shouye,R.drawable.collect,R.drawable.chazhao,R.drawable.gerenxinxi};
    int[] img2 = {R.drawable.shouye1,R.drawable.collect1,R.drawable.chazhao1,R.drawable.gerenxinxi1};
    int[] rbt = {R.id.rbt_shouye_bottom,R.id.rbt_collect_bottom,R.id.rbt_search_bottom,R.id.rbt_me_bottom};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        user_id = getIntent().getIntExtra("user_id",1);
        //初始化sp
        sp = getSharedPreferences("cart_json",MODE_PRIVATE);
        String cartJson = sp.getString("cart_json_" + user_id, "");
        if (!cartJson.isEmpty()){
            //初始化购物车
            cart = new GsonBuilder().setDateFormat("MMM dd, yyyy, hh:mm:ss a").create().fromJson(cartJson,new TypeToken<Map<Integer,Map<Integer,Integer>>>(){}.getType());
        }

        //初始化页面上的单选按钮
        rbt_shouye_bottom = findViewById(R.id.rbt_shouye_bottom);
        rbt_collect_bottom = findViewById(R.id.rbt_collect_bottom);
        rbt_search_bottom = findViewById(R.id.rbt_search_bottom);
        rbt_me_bottom = findViewById(R.id.rbt_me_bottom);
        //初始化四个子页面
        shopListFragment = new ShopListFragment();
        collectFragment = new CollectFragment();
        searchFragment = new SearchFragment();
        meFragment = new MeFragment();
        //初始化子页面管理器
        manager = this.getSupportFragmentManager();
        //给单选按钮添加点击事件
        rbt_shouye_bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换子页面
                changeFragment(shopListFragment,null,"shoplist");
                //切换单选按钮的图片
                changeRadioButtonImage(v.getId());
            }
        });
        rbt_collect_bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("page","shop");
                 collectFragment = new CollectFragment();
                changeFragment(collectFragment,bundle,"collect");
                changeRadioButtonImage(v.getId());
            }
        });
        rbt_search_bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragment(searchFragment,null,"search");
                changeRadioButtonImage(v.getId());
            }
        });
        rbt_me_bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragment(meFragment,null,"me");
                changeRadioButtonImage(v.getId());
            }
        });

        //刚进入时跳转到首页
        //切换子页面
        changeFragment(shopListFragment,null,"shoplist");
        //切换单选按钮的图片
        changeRadioButtonImage(rbt_shouye_bottom.getId());
    }
    //切换子页面功能函数
    public void changeFragment(Fragment fragment,Bundle bundle,String tag){
        //如果要切换的fragment为当前的页面则不做操作
        if(fragment == manager.findFragmentById(R.id.linear_fragment)){
            return;
        }

        //清空子页面栈
        for(int i=0,count = manager.getBackStackEntryCount();i<count;i++){
            manager.popBackStack();
        }
        //创建一个子页面的事务，以便切换子页面
        FragmentTransaction transaction = manager.beginTransaction();
        //设置子页面的初始化参数
        fragment.setArguments(bundle);
        //切换子页面，让子页面显示到主页面的指定位置
        transaction.add(R.id.linear_fragment,fragment);
        //给栈中的子页面一个标签
        transaction.addToBackStack(tag);
        //提交事务
        transaction.commit();
    }
    //切换单选按钮上的图片
    public void changeRadioButtonImage(int id){
        if (id == R.id.rbt_shouye_bottom) {
            changeImage(0);
        } else if (id == R.id.rbt_collect_bottom) {
            changeImage(1);
        } else if (id == R.id.rbt_search_bottom) {
            changeImage(2);
        } else if (id == R.id.rbt_me_bottom) {
            changeImage(3);
        }
    }
    //切换第几个单选按钮的图片
    public void changeImage(int index){
        for(int i=0;i<4;i++){
            if(i==index){
                RadioButton button = findViewById(rbt[i]);
                button.setCompoundDrawablesWithIntrinsicBounds(0,img2[i],0,0);
            }else{
                RadioButton button = findViewById(rbt[i]);
                button.setCompoundDrawablesWithIntrinsicBounds(0,img1[i],0,0);
            }
        }
    }
}